/**
 * Copyright (c) 2010 Jesco Freund.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * 3. Neither the name of Backtory nor the names of its contributors may be used
 *    to endorse or promote products derived from this software without
 *    specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *  $Id$
 */


#include <errno.h>
#include <fcntl.h>
#include <stddef.h>
#include <stdint.h>
#include <unistd.h>

#include "_endianess.h"
#include "_fheader.h"
#include "_file64.h"


extern uint16_t
yar_fheader_processingflag(yar_handle_t *handle)
{
	uint16_t result = 0;
	
	if (handle->compress_mdt)	{ result |= (uint16_t)LIBYAR_FH_MDT_COMPRESS;	}
	if (handle->encrypt_mdt)	{ result |= (uint16_t)LIBYAR_FH_MDT_ENCRYPT;	}
	if (handle->compress_fmd)	{ result |= (uint16_t)LIBYAR_FH_FMD_COMPRESS;	}
	if (handle->encrypt_fmd)	{ result |= (uint16_t)LIBYAR_FH_FMD_ENCRYPT;	}
	if (handle->digest_fmd)		{ result |= (uint16_t)LIBYAR_FH_FMD_DIGEST;		}
	if (handle->compress_fcd)	{ result |= (uint16_t)LIBYAR_FH_FCD_COMPRESS;	}
	if (handle->encrypt_fcd)	{ result |= (uint16_t)LIBYAR_FH_FCD_ENCRYPT;	}
	if (handle->digest_fcd)		{ result |= (uint16_t)LIBYAR_FH_FCD_DIGEST;		}
	if (handle->compress_fi)	{ result |= (uint16_t)LIBYAR_FH_FI_COMPRESS;	}
	if (handle->encrypt_fi)		{ result |= (uint16_t)LIBYAR_FH_FI_ENCRYPT;		}
	
	return result;
}


extern int
yar_fheader_set_buf(void *buf, size_t len, yar_handle_t *handle)
{
	unsigned char *ptr = (unsigned char *)buf;
	uint64_t identifier = yar_hton64((((uint64_t)LIBYAR_FH_IDENTIFIER) << 40) | ((uint64_t)handle->version));
	uint16_t pflag = yar_fheader_processingflag(handle);
	int i;
	
	if (len < LIBYAR_FH_BYTES)
		return ERANGE;
}


extern int
yar_fheader_set(yar_handle_t *handle)
{
	int rval = 0;
	unsigned char buffer[LIBYAR_FH_BYTES];
	rval = yar_fheader_set_buf(&buffer[0], LIBYAR_FH_BYTES, handle);
	if (rval)
		return rval;
	
	if (yar_file64_seek(handle->fd, 0, SEEK_SET) != 0)
		return errno;
	
	if (write(handle->fd, buffer, LIBYAR_FH_BYTES) < 0)
		return errno;
	
	return rval;
}

extern int
yar_fh_read_buf(const unsigned char *buf, size_t len, yar_header_t *header)
{
	if (len < 8)
	{
		header->valid = 0;
		return ERANGE;
	}
	
	/* extract the first three bytes from the buffer */
	header->identifier = (yar_ntoh32(*((uint32_t *)&buf[0])) >> 8);
	
	/* extract the version from the buffer */
	header->version = (((uint64_t)yar_ntoh32(*((uint32_t *)&buf[3]))) << 8) | ((uint64_t)buf[7]);
	
	/* check whether the buffer designates a valid yar file */
	if ((header->identifier == LIBYAR_FH_IDENTIFIER) && (header->version >= 0x0100000000ULL))
		header->valid = 1;
	else
		header->valid = 0;
	
	return (header->valid == 1) ? 0 : -1;
}


extern int
yar_fh_read_fd(int fd, yar_header_t *header)
{
	unsigned char buf[8];
	ssize_t rbytes;
	
	/* go to the beginning of the file */
	if (yar_file64_seek(fd, 0, SEEK_SET) < 0)
		return errno;
	
	/* try to read the 8 header bytes */
	if ((rbytes = read(fd, buf, 8)) < 0)
		return errno;
	
	/* if there are not at least 8 header bytes, something's wrong */
	if (rbytes != 8)
	{
		header->valid = 0;
		return -1;
	}
	
	return yar_fh_read_buf(buf, 8, header);
}


extern int
yar_fh_read_fn(const char *filename, yar_header_t *header)
{
	int fd, rval;
	
	fd = yar_file64_open(filename, O_RDONLY);
	if (fd < 0)
		return errno;
	
	rval = yar_fh_read_fd(fd, header);
	yar_file64_close(fd);
	
	return rval;
}


extern int
yar_fh_write_buf(void *buf, size_t len, yar_header_t *header)
{
	unsigned char *ptr = (unsigned char *)buf;
	uint64_t res = yar_hton64((((uint64_t)header->identifier) << 40) | ((uint64_t)header->version));
	int i;
	
	if (len < LIBYAR_FH_BYTES)
		return ERANGE;
	
	for (i = 0; i < 8; i++)
		ptr[i] = *(((unsigned char *)&res) + i);
	
	return 0;
}
